package com.schedule.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.schedule.entity.Count;
import com.schedule.service.CountService;

@Controller
public class CountController {
	@Autowired
	CountService countService;
	
	@ResponseBody
	@RequestMapping(value="/findAll.action",produces = "text/json;charset=UTF-8")
	public String findAll(HttpServletRequest request, HttpServletResponse response) {
		String param_data=request.getParameter("param");
		JsonParser jp=new JsonParser();
		JsonObject jo_data=jp.parse(param_data).getAsJsonObject();
		String user=jo_data.get("user").getAsString();
		List<Count> list=countService.selectAll(user);
		Gson gson=new Gson();
		String gson1=gson.toJson(list);
		return gson1;
	}
	
	@RequestMapping("/AddCount.action")
	public void AddCount(HttpServletRequest request,Count count,HttpServletResponse response) throws IOException {
		
		String end_minute=request.getParameter("end_minute");
		String end_hour=request.getParameter("end_hour");
		count.setEnd_time(count.getEnd_time()+" "+end_hour+":"+end_minute);
		countService.AddCount(count);
		response.sendRedirect("jsp/backtime.jsp?code=1&user="+count.getUser());
	}

	@RequestMapping("/DeleteCount.action")
	public void DeleteCount(HttpServletRequest request,HttpServletResponse response) throws IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Count count=countService.SelectById(id);
		countService.DeleteCount(id);
		
		response.sendRedirect("jsp/backtime.jsp?code=1&user="+count.getUser());
	}
	@RequestMapping("/SelectOneCount.action")
	public void SelectOneCount(HttpServletRequest request,HttpServletResponse response) throws IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Count count=countService.SelectById(id);
		String end_time=count.getEnd_time();
		String[] str=end_time.split("\\s+");
		String date=str[0];
		/*System.out.println(date);*/
		String time=str[1];
		/*System.out.println(time);*/
		String[] str2=time.split(":");
		String hour=str2[0];
		String minute=str2[1];
		/*System.out.println(hour);
		System.out.println(minute);*/
		HttpSession session=request.getSession();
		session.setAttribute("date", date);
		session.setAttribute("hour", hour);
		session.setAttribute("minute", minute);
		session.setAttribute("count", count);
		response.sendRedirect("jsp/editCount.jsp?code=1&user="+count.getUser());
	}
	@RequestMapping("/UpdateCount.action")
	public void UpdateCount(HttpServletRequest request,HttpServletResponse response,Count count) throws IOException {
		String end_minute=request.getParameter("end_minute");
		String end_hour=request.getParameter("end_hour");
		count.setEnd_time(count.getEnd_time()+" "+end_hour+":"+end_minute);
		countService.UpdateCount(count);
		response.sendRedirect("jsp/backtime.jsp?code=1&user="+count.getUser());
	}
	
	@ResponseBody
	@RequestMapping(value="/findAlltimeback.action",produces = "text/json;charset=UTF-8")
	public String findAlltimeback(HttpServletRequest request, HttpServletResponse response) {
		String param_data=request.getParameter("param");
		JsonParser jp=new JsonParser();
		JsonObject jo_data=jp.parse(param_data).getAsJsonObject();
		String user=jo_data.get("user").getAsString();
		String now_time=jo_data.get("now_time").getAsString();
//		System.out.println(user);
//		System.out.println(now_time);
		List<Count> list=countService.selectAlltime(user, now_time);
		Gson gson=new Gson();
		String gson1=gson.toJson(list);
		//System.out.println(gson1);
		return gson1;
	}

}
